/* Rui Santos Complete project details at https://RandomNerdTutorials.com/esp-now-esp8266-nodemcu-arduino-ide/ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files. The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. */ #include // Enable OTA programming 1 of 3 lines WiFiServer server(80); #include #include // REPLACE WITH RECEIVER MAC Address uint8_t broadcastAddress[] = {0x5C, 0xCF, 0x7F, 0x4C, 0x86, 0x54}; // change to MAC address of receiver int counter; // Structure example to send data // Must match the receiver structure typedef struct struct_message { char a[32]; int b; float c; String d; bool e; } struct_message; // Create a struct_message called myData struct_message myData; unsigned long lastTime = 0; unsigned long timerDelay = 10 //any lower stops OTA uploads ; // send readings timer // Callback when data is sent void OnDataSent(uint8_t *mac_addr, uint8_t sendStatus) { Serial.print("Last Packet Send Status: "); if (sendStatus == 0){ Serial.println("Delivery success"); } else{ Serial.println("Delivery fail"); } } void setup() { ArduinoOTA.begin(); // Enable OTA programming 2 of 3 lines // Init Serial Monitor Serial.begin(115200); // Set device as a Wi-Fi Station WiFi.mode(WIFI_STA); // Init ESP-NOW if (esp_now_init() != 0) { Serial.println("Error initializing ESP-NOW"); return; } // Once ESPNow is successfully Init, we will register for Send CB to // get the status of Trasnmitted packet esp_now_set_self_role(ESP_NOW_ROLE_CONTROLLER); esp_now_register_send_cb(OnDataSent); // Register peer esp_now_add_peer(broadcastAddress, ESP_NOW_ROLE_SLAVE, 1, NULL, 0); } void loop() { ArduinoOTA.handle(); // Enable OTA programming 3 of 3 lines if ((millis() - lastTime) > timerDelay) { // Set values to send strcpy(myData.a, "THIS IS A CHAR"); myData.b = analogRead(0); // was random(1,20); myData.c = counter; myData.d = "Hello"; myData.e = false; // Send message via ESP-NOW esp_now_send(broadcastAddress, (uint8_t *) &myData, sizeof(myData)); counter = counter + 1; if (counter > 10000.0) counter = 0; lastTime = millis(); } }